package com.yds.developtools

import android.content.Context
import com.yds.developtools.utils.YDSInternalStorageHelper.getAppFilesDir
import com.yds.developtools.utils.YDSLogHelper
import com.yds.developtools.utils.https.HttpResponse
import com.yds.developtools.utils.https.SHttp
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File

class SHttpTips {

    private lateinit var sHttp: SHttp

    fun init() {
        sHttp = SHttp.instance.setBaseUrl("https://medical.baichensaas.com")
            .enableLogging(true)
            .trustAllCertificates(true)// 设置true表示信任所有证书，可以不设置下面证书，false标识必须要验证指定证书。
//            .setCertificate(assets.open("certificate.crt"), "optional-password")
    }

    fun requestMethods() {
        CoroutineScope(Dispatchers.Default).launch {
            val response = sHttp.get("/api/v1/app/errorLog/list", mapOf("type" to "1", "page" to "1", "pagesize" to "100"), responseType = String::class)
            when (response) {
                is HttpResponse.Success -> {
                    YDSLogHelper.i("Success (${response.code}): ${response.body}")
                }

                is HttpResponse.Error -> {
                    YDSLogHelper.i("Error (${response.code}): ${response.message}")
                }

                is HttpResponse.Exception -> {
                    YDSLogHelper.i("Exception: ${response.exception.message}")
                }
            }
        }
    }

    fun downFile(apkUrl: String?, context: Context) {
        CoroutineScope(Dispatchers.Default).launch {
            val apkUrl = "https://files.baichensaas.com/baichenjiankang/file/app/f8675f0ec64948091a08a47c250e6706.apk"
            sHttp.downloadFile(apkUrl, context.getAppFilesDir() + "/file.apk") { progress: Int ->
                YDSLogHelper.i("Download progress: $progress%")
            }.also { response ->
                when (response) {
                    is HttpResponse.Success -> {
                        YDSLogHelper.i("Success (${response.code}): ${response.body}")
                    }

                    is HttpResponse.Error -> {
                        YDSLogHelper.i("Error (${response.code}): ${response.message}")
                    }

                    is HttpResponse.Exception -> {
                        YDSLogHelper.i("Exception: ${response.exception.message}")
                    }
                }
            }
        }
    }

    fun uploadFile(context: Context) {
        CoroutineScope(Dispatchers.Default).launch {
            sHttp.uploadFile(
                "https://medical.baichensaas.com/api/v1/app/files/upload", File(context.getAppFilesDir() + "/file.apk"), params = mapOf("type" to "1")
            ) { progress: Int ->
                YDSLogHelper.i("Upload progress: $progress%")
            }.also { response ->
                when (response) {
                    is HttpResponse.Success -> {
                        YDSLogHelper.i("Success (${response.code}): ${response.body}")
                    }

                    is HttpResponse.Error -> {
                        YDSLogHelper.i("Error (${response.code}): ${response.message}")
                    }

                    is HttpResponse.Exception -> {
                        YDSLogHelper.i("Exception: ${response.exception.message}")
                    }
                }
            }
        }
    }

    fun destroy() {
        // 终止指定的网络请求【请求的接口地址】
        sHttp.cancelRequest("请求的接口地址")
        // 终止所有网络请求
        sHttp.cancelAllRequests()
    }
}